//
//  PBHomeBestCell.m
//  PBHomeDemo
//
//  Created by Playboy on 2018/3/13.
//  Copyright © 2018年 tmkj. All rights reserved.
//

#import "PBHomeBestCell.h"
#import "PBBestCollectionViewCell.h"
#import "PBBestHomeModel.h"
#import "PBRentTypeController.h"
#import "PBHomeDetailController.h"

static NSString * const kPBBestCollectionViewCell = @"PBBestCollectionViewCell";

@interface PBHomeBestCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (strong, nonatomic) IBOutlet UIButton *moreButton;
@property (strong, nonatomic) IBOutlet UILabel *typeNameLabel;
- (IBAction)moreButtonClick:(UIButton *)sender;
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray    *dataSource;

@end

@implementation PBHomeBestCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self configure];
}
- (void)configure {
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.itemSize = CGSizeMake(200, 230);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 10;
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:kPBBestCollectionViewCell bundle:nil] forCellWithReuseIdentifier:kPBBestCollectionViewCell];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)refreshWithIndexPath:(NSIndexPath *)indexPath {
    self.moreButton.tag = indexPath.row + 100;
    NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
    [param setValue:@"1" forKey:@"currentPage"];
    [param setValue:@"10" forKey:@"pageSize"];
    if (indexPath.row == 0) {
        self.typeNameLabel.text = @"荣者公寓";
        [param setObject:@"true" forKey:@"apartment"];
    }else if (indexPath.row == 1) {
        self.typeNameLabel.text = @"合租";
        [param setObject:@"false" forKey:@"entireRent"];
    }else if (indexPath.row == 2) {
        self.typeNameLabel.text = @"整租";
        [param setObject:@"true" forKey:@"entireRent"];
    }else if (indexPath.row == 3) {
        self.typeNameLabel.text = @"直租";
        [param setObject:@"true" forKey:@"ownerRent"];
    }
    [TMNetwork requestPostWithParam:param url:@"/house/query" callback:^(TMBaseReqModel *model) {
        if (model.code == 200) {
            self.dataSource = [PBBestHomeModel objectArrayWithKeyValuesArray:model.data];
            [self.collectionView reloadData];
        }else {
            [self showHint:model.message];
        }
    }];
}

#pragma mark - <UICollectionViewDelegate,UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PBBestCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kPBBestCollectionViewCell forIndexPath:indexPath];
    [cell refreshWithModel:self.dataSource[indexPath.row]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    PBBestHomeModel *model = self.dataSource[indexPath.row];
    PBHomeDetailController *vc = [[PBHomeDetailController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.houseId = [NSString stringWithFormat:@"%zd",model.ID];
    [self.jumpVC.navigationController pushViewController:vc animated:YES];
}

- (IBAction)moreButtonClick:(UIButton *)sender {
    PBRentTypeController *vc = [[PBRentTypeController alloc] init];
    if (sender.tag == 100) {
        // 荣者公寓
        vc.type = 4;
    }else if (sender.tag == 101) {
        // 合租
        vc.type = 1;
    }else if (sender.tag == 102) {
        // 整租
        vc.type = 2;
    }else if (sender.tag == 103) {
        // 直租
        vc.type = 3;
    }
    vc.hidesBottomBarWhenPushed = YES;
    [self.jumpVC.navigationController pushViewController:vc animated:YES];
}

@end
